package cn.wx;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class ESTest {

        //获取ES连接            调用方法之后使用get() 方法 存入到ES中
    TransportClient client = ESClientUtil.getClient();


    //添加
    @Test
    public void testAdd(){
        // 2.准备要放入到ES中的数据
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("name","王天霸");
        map.put("sex",1);
        map.put("id",5L);
        // 3.使用链式编程，将数据存入到ES中
        IndexRequestBuilder Builder = client.prepareIndex("pethome", "pet", "1").setSource(map);
        // 4.得到添加文档结果
        System.out.println(Builder.get());
        // 5.关闭连接
        client.close();
    }

    /*
    *   获取查看
    * */
    @Test
    public void testGet(){
        // 2.获取指定文档
        GetResponse documentFields = client.prepareGet("pethome", "pet", "1").get();
        System.out.println(documentFields.getSource());

        client.close();
    }

    /*
    *   修改
    */
    @Test
    public void testUpd(){
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "pet", "1");

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("name","古天乐");
        map.put("sex",2);
        // 3.使用链式编程，将要修改的数据存入到ES中
        updateRequestBuilder.setDoc(map).get();

        client.close();
    }

    /*
    *   删除
    * */
    @Test
    public void testDel(){
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "pet", "1");
        deleteRequestBuilder.get();
    }


 /*
 *     - 查询用户表
        - name包含：我在源码
        - age在1~12之间
        - sex=1
        - 每页大小2
        - 从2页开始查
        - 按照age倒序
 * */

    @Test
    public void test(){
        SearchRequestBuilder searchRequestBuilder = client.prepareSearch("pethome");
            //拿到boolQuery
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "我在源码");
        BoolQueryBuilder must = boolQueryBuilder.must(matchQueryBuilder);

        BoolQueryBuilder sex = boolQueryBuilder.filter(QueryBuilders.termQuery("sex", 1));
        sex.filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));

        searchRequestBuilder.setQuery(boolQueryBuilder);

        //每页查询两天数据
        searchRequestBuilder.setSize(2);
        //从第二页开始查询呢
        searchRequestBuilder.setFrom(2);
        //排序
        searchRequestBuilder.addSort("age", SortOrder.DESC);

        SearchHit[] hits = searchRequestBuilder.get().getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }
        //关流
        client.close();
    }












    /**
     * @Description: 批量添加数据
     * @Author: Director
     * @Date: 2022/7/23 0:02
     * @return: void
     **/
    @Test
    public void testBatch() throws Exception {
        // 1.获取ES连接
        TransportClient client = ESClientUtil.getClient();
        // 2.创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 3.批量创建文档，放入批量操作对象中
        for (int i = 2; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "我在源码学习Java!" + i)
                            .field("age", i - 1)
                            .field("sex", i%2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 4.关闭连接
        client.close();
    }

}
